package com.shopping.servlet;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.shopping.bean.Goods;
import com.shopping.service.GoodsService;
import com.shopping.service.impl.GoodsServiceImpl;



/**
 * @author 2165435274
 *
 */
@WebServlet("/addGoods.do")
public class GoodsServletAdd extends HttpServlet {
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req,resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		FileItemFactory factory = new DiskFileItemFactory();//创建了一个 文件组件工厂。
		ServletFileUpload upload = new ServletFileUpload(factory);// 创建一个 文件上传核心组件
		upload.setHeaderEncoding("UTF-8");//设置 核心组件请求头部份的编码 UTF-8
		if(upload.isMultipartContent(req)) {
			int busineId = 0;
			String gName = null;
			int gPrice = 0;
			String gType = null;
			String gIntro = null;
			String gIf = null;
			String savePath = null;
			String fileName = null;
			try {
				List<FileItem> items =upload.parseRequest(req);
				for(FileItem item:items) {
					if(item.isFormField()) {
						if("gName".equals(item.getFieldName())){
							gName = item.getString("UTF-8");
						}
						if("gType".equals(item.getFieldName())){
							gType = item.getString("UTF-8");
						}
						if("busineId".equals(item.getFieldName())){
							try {
								busineId = Integer.parseInt(item.getString("UTF-8"));
							}catch(Exception o) {
								req.setAttribute("msg", "请输入数字");
								req.getRequestDispatcher("addGoods.jsp").forward(req, resp);
								return;
							}
						}
						if("gPrice".equals(item.getFieldName())){
							try {
								gPrice = Integer.parseInt(item.getString("UTF-8"));
							}catch(Exception o) {
								req.setAttribute("msg", "价格请输入数字");
								req.getRequestDispatcher("addGoods.jsp").forward(req, resp);
								return;
							}
						}
						if("gIntro".equals(item.getFieldName())){
				
								gIntro = item.getString("UTF-8");
							
						}
						if("gIf".equals(item.getFieldName())){
							gIf = item.getString("UTF-8");
						}
						
					}else {
						//文件类型验证
						if(!item.getContentType().contains("image")) {
							req.setAttribute("msg", "请选择图片文件");
							req.getRequestDispatcher("addGoods.jsp").forward(req, resp);
							return;
						}
						//文件大小限制
						if(item.getSize() >1024 * 1024) {
							req.setAttribute("msg", "文件过大，请选择小于1m的图片上传");
							req.getRequestDispatcher("addGoods.jsp").forward(req, resp);
							return;
						}
					
						String realPath = "c://womendress";//真正存在服务器的地址（一般不要直接存在 项目内）
						File folder = new File(realPath);
						if(!folder.exists()) {
							folder.mkdirs();
						}
						//2.生成一个 独一无问的文件名 :1 文件名(UUID 或者是时间戳)+文件的原始的后缀名
						fileName = System.currentTimeMillis()+item.getName().substring(item.getName().lastIndexOf("."));
						File desFile = new File(realPath,fileName);
						System.out.println(desFile); 
						try {
							item.write(desFile);//把用户上传的文件 真实的存在 服务器的某个地址。
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
					//把用户信息存入数据库，此步骤略
					
				}
				
				
				GoodsService gs = new GoodsServiceImpl();
				
				Goods goods = new Goods(gName,gPrice,gType,gIntro,gIf,busineId);
				savePath = "/shopping/images/womendress/"+fileName;//项目路径
				
				goods.setgImage(savePath);
				gs.addGoods(goods);
				resp.sendRedirect("addGoods.jsp");
				
			} catch (FileUploadException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else {
			
		}
	}

}
